/*******************************************************************************
*                                                                              *
*   PrimeSense NITE 1.3 - Point Viewer Sample                                  *
*   Copyright (C) 2010 PrimeSense Ltd.                                         *
*                                                                              *
*******************************************************************************/

#ifndef XNV_POINT_DRAWER_H_
#define XNV_POINT_DRAWER_H_

#include <map>
#include <list>
#include <openni/XnCppWrapper.h>
#include <nite/XnVPointControl.h>

typedef enum
{
    IN_SESSION,
    NOT_IN_SESSION,
    QUICK_REFOCUS
} SessionState;

void PrintSessionState(SessionState eState);
/**
 * This is a point control, which stores the history of every point
 * It can draw all the points as well as the depth map.
 */
class XnVPointDrawer : public XnVPointControl
{
public:
    XnVPointDrawer(XnUInt32 nHistorySize, xn::DepthGenerator depthGenerator);
    virtual ~XnVPointDrawer();

    XnPoint3D pt3D;
    XnPoint3D ptProjective;
    XnBool handFound;

    /**
     * Handle a new message.
     * Calls other callbacks for each point, then draw the depth map (if needed) and the points
     */
    void Update(XnVMessage* pMessage);

    /**
     * Handle creation of a new point
     */
    void OnPointCreate(const XnVHandPointContext* cxt);
    /**
     * Handle new position of an existing point
     */
    void OnPointUpdate(const XnVHandPointContext* cxt);
    /**
     * Handle destruction of an existing point
     */
    void OnPointDestroy(XnUInt32 nID);

    /**
     * Draw the points, each with its own color.
     */
    void Draw() const;

    /**
     * Change mode - should draw the depth map?
     */
    void SetDepthMap(XnBool bDrawDM);
    /**
     * Change mode - print out the frame id
     */
    void SetFrameID(XnBool bFrameID);

    void SetTouchingFOVEdge(XnUInt32 nID);
protected:
    XnBool IsTouching(XnUInt32 nID) const;
    // Number of previous position to store for each hand
    XnUInt32 m_nHistorySize;
    // previous positions per hand
    std::map<XnUInt32, std::list<XnPoint3D> > m_History;
    std::list<XnUInt32> m_TouchingFOVEdge;
    // Source of the depth map
    xn::DepthGenerator m_DepthGenerator;
    XnFloat* m_pfPositionBuffer;

    XnBool m_bDrawDM;
    XnBool m_bFrameID;
};

#endif
